﻿using DAL;
using DianShangHouTai.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace DianShangHouTai.Controllers
{
    public class GuiGeController : Controller
    {
        ShopContext db = new ShopContext();
        // GET: GuiGe商品规格管理
        public ActionResult Index()
        {
            return View();
        }     
        /// <summary>
        /// 规格查询方法
        /// </summary>
        /// <returns></returns>
        public ActionResult Indexs()
        {
                List<GuiGeZhi> users = db.GuiGeZhi.Where(p => p.GuiGeId==1).ToList();
                return Content(Pager.PagedData<GuiGeZhi>(users, users.Count()));
        }
        /// <summary>
        /// 颜色管理
        /// </summary>
        /// <returns></returns>
        public ActionResult Colour()
        {
            return View();
        }
        /// <summary>
        /// 添加规格颜色
        /// </summary>
        /// <returns></returns>
        public ActionResult GuiGeAdd_Colour()
        {
            var lists = db.GuiGe.Select(p => new SelectListItem { Value = p.Id.ToString(), Text = p.GuiGeName }).ToList();
            return View(lists);
        }
        [HttpPost]
        public ActionResult GuiGeAdd_Colour(UIGuiGeAdd data)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.GuiGeZhi.Add(new GuiGeZhi
                    {
                        GuiGeZhiName = data.GuiGeZhiName,
                        GuiGeId=data.GuiGeId
                       
                    });

                    if (db.SaveChanges() > 0)
                    {
                        return Json(new UIResult(true, ""));
                    }
                    else
                    {
                        return Json(new UIResult(true, "未知原因添加失败"));
                    }
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    int i = 1;
                    foreach (var s in ModelState.Values)
                    {
                        foreach (var p in s.Errors)
                        {
                            sb.AppendFormat("{0}-{1};", i++, p.ErrorMessage);
                        }
                    }
                    return Json(new UIResult(false, "服务端验证未通过," + sb));
                }

            }
            catch (Exception ex)
            {
                return Json(new UIResult(false, ex.Message));
            }

        }
        /// <summary>
        /// 颜色修改
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActionResult GuiGeEdit_Colour(int? Id)
        {
            GuiGeZhi ast = db.GuiGeZhi.SingleOrDefault(x => x.Id == Id);
            return View(ast);
        }
        [HttpPost]
        public ActionResult GuiGeEdit_Colour(UIGuiGeAdd data)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var obj = db.GuiGeZhi.SingleOrDefault(p => p.Id == data.Id);
                obj.GuiGeZhiName = data.GuiGeZhiName;
              
                if (db.SaveChanges() > 0)
                {
                    return Json(new UIResult(true, ""));
                }
                else
                {
                    return Json(new UIResult(false, "未知原因修改失败"));
                }
            }
                else
                {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                int i = 1;
                foreach (var s in ModelState.Values)
                {
                    foreach (var p in s.Errors)
                    {
                        sb.AppendFormat("{0}-{1};", i++, p.ErrorMessage);
                    }
                }
                return Json(new UIResult(false, "服务端验证未通过," + sb));

            }
        }
            catch (Exception ex)
            {

                return Json(new UIResult(false, ex.Message));
            }
        }
        /// <summary>
        /// 颜色删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GuiGedel_Colour(int? id)
        {
            try
            {
                var obj = db.GuiGeZhi.SingleOrDefault(p => p.Id == id);
                    db.GuiGeZhi.Remove(obj);
                if (db.SaveChanges() > 0)
                    return Json(new UIResult(true, "删除成功"));
                else
                    return Json(new UIResult(false, "要删除的数据可能不存在"));
            }
            catch (Exception ex)
            {

                return Json(new UIResult(false, "出错了" + ex.Message));
            }
        }
        /// <summary>
        /// 内存管理
        /// </summary>
        /// <returns></returns>
        public ActionResult Memory()
        {
            return View();
        }
        /// <summary>
        /// 规格查询方法
        /// </summary>
        /// <returns></returns>
        public ActionResult Memorys()
        {
            List<GuiGeZhi> users = db.GuiGeZhi.Where(p => p.GuiGeId == 2).ToList();
            return Content(Pager.PagedData<GuiGeZhi>(users, users.Count()));
        }
        /// <summary>
        /// 添加内存颜色
        /// </summary>
        /// <returns></returns>
        public ActionResult GuiGeAdd_Memory()
        {
            var lists = db.GuiGe.Select(p => new SelectListItem { Value = p.Id.ToString(), Text = p.GuiGeName }).ToList();
            return View(lists);
        }
        [HttpPost]
        public ActionResult GuiGeAdd_Memory(UIGuiGeAdd data)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.GuiGeZhi.Add(new GuiGeZhi
                    {
                        GuiGeZhiName = data.GuiGeZhiName,
                        GuiGeId = data.GuiGeId

                    });

                    if (db.SaveChanges() > 0)
                    {
                        return Json(new UIResult(true, ""));
                    }
                    else
                    {
                        return Json(new UIResult(true, "未知原因添加失败"));
                    }
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    int i = 1;
                    foreach (var s in ModelState.Values)
                    {
                        foreach (var p in s.Errors)
                        {
                            sb.AppendFormat("{0}-{1};", i++, p.ErrorMessage);
                        }
                    }
                    return Json(new UIResult(false, "服务端验证未通过," + sb));
                }

            }
            catch (Exception ex)
            {
                return Json(new UIResult(false, ex.Message));
            }

        }
        /// <summary>
        /// 内存修改
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActionResult GuiGeEdit_Memory(int? Id)
        {
            GuiGeZhi ast = db.GuiGeZhi.SingleOrDefault(x => x.Id == Id);
            return View(ast);
        }
        [HttpPost]
        public ActionResult GuiGeEdit_Memory(UIGuiGeAdd data)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var obj = db.GuiGeZhi.SingleOrDefault(p => p.Id == data.Id);
                    obj.GuiGeZhiName = data.GuiGeZhiName;

                    if (db.SaveChanges() > 0)
                    {
                        return Json(new UIResult(true, ""));
                    }
                    else
                    {
                        return Json(new UIResult(false, "未知原因修改失败"));
                    }
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    int i = 1;
                    foreach (var s in ModelState.Values)
                    {
                        foreach (var p in s.Errors)
                        {
                            sb.AppendFormat("{0}-{1};", i++, p.ErrorMessage);
                        }
                    }
                    return Json(new UIResult(false, "服务端验证未通过," + sb));

                }
            }
            catch (Exception ex)
            {

                return Json(new UIResult(false, ex.Message));
            }
        }
        /// <summary>
        /// 内存删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GuiGeMemory_Colour(int? id)
        {
            try
            {
                var obj = db.GuiGeZhi.SingleOrDefault(p => p.Id == id);
                db.GuiGeZhi.Remove(obj);
                if (db.SaveChanges() > 0)
                    return Json(new UIResult(true, "删除成功"));
                else
                    return Json(new UIResult(false, "要删除的数据可能不存在"));
            }
            catch (Exception ex)
            {

                return Json(new UIResult(false, "出错了" + ex.Message));
            }
        }
        /// <summary>
        /// 储存管理
        /// </summary>
        /// <returns></returns>
        public ActionResult Storage()
        {
            return View();
        }
        /// <summary>
        /// 规格管理查询方法
        /// </summary>
        /// <returns></returns>
        public ActionResult StorageS()
        {
            List<GuiGeZhi> lis = db.GuiGeZhi.Where(p => p.GuiGeId == 3).ToList();
            return Content(Pager.PagedData<GuiGeZhi>(lis, lis.Count()));
        }
        /// <summary>
        /// 规格添加
        /// </summary>
        /// <returns></returns>
        public ActionResult GuiGeAdd_Storage()
        {
            var list = db.GuiGe.Select(p => new SelectListItem { Value = p.Id.ToString(), Text = p.GuiGeName }).ToList();
            return View(list);
        }
        [HttpPost]
        public ActionResult GuiGeAdd_Storage(UIGuiGeAdd data)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.GuiGeZhi.Add(new GuiGeZhi
                    {
                        GuiGeZhiName = data.GuiGeZhiName,
                        GuiGeId = data.GuiGeId

                    });

                    if (db.SaveChanges() > 0)
                    {
                        return Json(new UIResult(true, ""));
                    }
                    else
                    {
                        return Json(new UIResult(true, "未知原因添加失败"));
                    }
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    int i = 1;
                    foreach (var s in ModelState.Values)
                    {
                        foreach (var p in s.Errors)
                        {
                            sb.AppendFormat("{0}-{1};", i++, p.ErrorMessage);
                        }
                    }
                    return Json(new UIResult(false, "服务端验证未通过," + sb));
                }

            }
            catch (Exception ex)
            {
                return Json(new UIResult(false, ex.Message));
            }

        }
        /// <summary>
        /// 规格修改
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActionResult GuiGeEdit_Storage(int? Id)
        {
            GuiGeZhi ast = db.GuiGeZhi.SingleOrDefault(x => x.Id == Id);
            return View(ast);
        }
        [HttpPost]
        public ActionResult GuiGeEdit_Storage(UIGuiGeAdd data)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var obj = db.GuiGeZhi.SingleOrDefault(p => p.Id == data.Id);
                    obj.GuiGeZhiName = data.GuiGeZhiName;

                    if (db.SaveChanges() > 0)
                    {
                        return Json(new UIResult(true, ""));
                    }
                    else
                    {
                        return Json(new UIResult(false, "未知原因修改失败"));
                    }
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    int i = 1;
                    foreach (var s in ModelState.Values)
                    {
                        foreach (var p in s.Errors)
                        {
                            sb.AppendFormat("{0}-{1};", i++, p.ErrorMessage);
                        }
                    }
                    return Json(new UIResult(false, "服务端验证未通过," + sb));

                }
            }
            catch (Exception ex)
            {

                return Json(new UIResult(false, ex.Message));
            }
        }
    }
}